package com.conversor;

import java.util.ArrayList;
import java.util.List;

import com.core.Temperatura.TemperaturaBase;
import com.core.Temperatura.TemperaturaFactory;
import com.core.enumerators.TemperaturaType;
import com.conversor.R;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.GridView;
import android.widget.ListView;
import android.widget.Spinner;
import android.widget.TableLayout;
import android.widget.TableRow;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.TableRow.LayoutParams;

public class TemperaturaActivity extends ActivityBase {
	
	private TemperaturaType tipoSelecionadoEnum;
			  
	/** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.temperatura);       
                
        this.FillSpinner(R.id.dropTemp,tiposTemperaturaGrid);
        
        //Metodo do Spinner para capturar o item selecionado
        spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener(){
        	        	        	
        	@Override
        	public void onNothingSelected(AdapterView<?> parent){
        		
        	}

			@Override
			public void onItemSelected(AdapterView<?> parent, View v , int posicao, long id) {
				// //Pega nome pela posicao
				tipoSelecionado = parent.getItemAtPosition(posicao).toString();
        		//Imprime um Toast na tela com o nome que foi selecionado
        		//Toast.makeText(TemperaturaActivity.this, "Tipo Selecionado:" + tipoSelecionado, Toast.LENGTH_LONG).show();
        		        		        		
        		if(tipoSelecionado.equals(TemperaturaType.Celcius.toString())){
        			tipoSelecionadoEnum = TemperaturaType.Celcius;
        		}
        		else if(tipoSelecionado.equals(TemperaturaType.Kelvin.toString())){
        			tipoSelecionadoEnum = TemperaturaType.Kelvin;
        		}
        		else if(tipoSelecionado.equals(TemperaturaType.Fahrenheit.toString())){
        			tipoSelecionadoEnum = TemperaturaType.Fahrenheit;
        		}
			}        	
        });               
        FillTableLayout();
    }    
    
    /**
     * Fill TableLayout with values converted.
     */
    private void FillTableLayout(){
    	FillTableLayout("");
    }
    
    /**
     * Fill TableLayout with values converted.
     * @param valueToBeConverted
     */
    private void FillTableLayout(String valueToBeConverted){
    	
    	String valueConverted = "";
    	//Initialize The Table Layout.
    	InitializeTableLayoutById(R.id.tblTemp);
        
        TemperaturaBase tempObject = null; 
        if(!valueToBeConverted.equals("")){
        	tempObject = TemperaturaFactory.Create(tipoSelecionadoEnum, Float.parseFloat(valueToBeConverted));
                				
	        // Go through each item in the array
	        for (int current = 0; current < QuantidadeTipoTemperatura; current++)
	        {        	   
	        	if(!tiposTemperaturaGrid[current].equals(tipoSelecionadoEnum.toString())){
	        		
	        		if(tiposTemperaturaGrid[current].equals(TemperaturaType.Celcius.toString())){
	        			valueConverted = Double.toString(tempObject.ToCelcius());
	        		}
	        		else if(tiposTemperaturaGrid[current].equals(TemperaturaType.Kelvin.toString())){
	        			valueConverted = Double.toString(tempObject.ToKelvin());
	        		}
	        		else if(tiposTemperaturaGrid[current].equals(TemperaturaType.Fahrenheit.toString())){
	        			valueConverted = Double.toString(tempObject.ToFahrenheit());
	        		}       		
	        		//Fill the TableLayout(Row) with a value converted.
	        		FillTableRow(this, valueConverted, tiposTemperaturaGrid[current], current);     
	        	}
	        }   
        }
    }
    
    /**
     * Called when the user clicks the Send button, Method to Convert the value.
     * @param view
     */
    public void Converter(View view) {    	
    	EditText editText = (EditText) findViewById(R.id.txtTemp);
    	String message = editText.getText().toString();
    	if(!message.equals("")){
			//Toast.makeText(TemperaturaActivity.this, "Tipo Selecionado:" + message, Toast.LENGTH_LONG).show();
			FillTableLayout(message);
    	}else{
    		Toast.makeText(TemperaturaActivity.this, "Coloque um valor para ser convertido.", Toast.LENGTH_LONG).show();
    	}    	
    }    
}


